package com.dingwen.treasure.business.juc;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * 　FutureTaskApi: FutureTask 案例
 * 　@author dingwen
 * 　@date 2022/7/1
 */
@Slf4j
public class FutureTaskApi {

    @SneakyThrows
    public static void main(String[] args) {
        FutureTask<String> futureTask = new FutureTask<>(new Task());
        new Thread(futureTask).start();
        // 主线程
        log.info(Thread.currentThread().getName());
        // 阻塞
         String result = futureTask.get();
        log.info(result);
    }
}


class Task implements Callable<String> {

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public String call() throws Exception {
        TimeUnit.SECONDS.sleep(10);
        return "call";
    }
}
